//获取管理员列表
var table = $("#staff_List").dataTable({
    //"iDisplayLength":10,
    "ajax": {
        "url":"/role/get-admin-list",
        "type":'post',
        "data": function (d) {
            //添加额外的参数传给服务器
            //return $.extend({ CurrentPage:0, PageSize: 10}, {});
        }},
    'bPaginate': true,
    "bDestory": true,
    "bRetrieve": true,
    "bFilter":false,
    "bSort": true,
    "bProcessing": true,
    "autoWidth": false,
    "searching":true,
    "aoColumns": [
        {"mDataProp": "AdminName"},
        {"mDataProp": "AdminNameLower"},
        {"mDataProp": "RoleNames"},
        {
            "mDataProp": "AdminId",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html('<button type="button" class="btn btn-default btn-xs GetRoleList"  data-adminid="'+oData.AdminId+'"><i class="fa fa-cog"></i>设置</button>');

            }
        },
    ],
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
        //add selected class
        $(nRow).click(function () {
            if ($(this).hasClass('row_selected')) {
                $(this).removeClass('row_selected');
            } else {
                table.$('tr.row_selected').removeClass('row_selected');
                $(this).addClass('row_selected');
            }
        });
    },
    "fnInitComplete": function (oSettings, json) {
    }
});
//获取角色列表
$(document).on('click','.GetRoleList',function(){
    if ($("#action_List").hasClass("dataTable")) {
        dttable = $("#action_List").dataTable();
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
    }
    var AdminId = $(this).data('adminid');
    $("#SaveRelationShip").attr("data-adminid",AdminId);
    $(".mapbtn").show();
    var actionTable = $("#action_List").dataTable({
        "ajax": {
            "url":"/role/get-admin-role-map",
            "type":'post',
            "data": function (d) {
                //添加额外的参数传给服务器
                return  {AdminId:AdminId};
            }},
        'bPaginate': true,
        "bDestory": true,
        "bRetrieve": true,
        "bFilter":false,
        "bSort": true,
        "bProcessing": true,
        "autoWidth": false,
        "searching":true,
        "aoColumns": [
            {"mDataProp": "RoleId",
                'fnCreatedCell':function(nTd, sData, oData, iRow, iCol){
                    $(nTd).html('<i class="themeicon-custom fa fa-square-o '+ (oData.IsSelect ? 'selected' : '')+'"data-roleid="'+ oData.RoleId+'"></i>');
                }
            },
            {"mDataProp": "RoleName"},
            {"mDataProp": "RoleRemark"},
        ],
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            //add selected class
            $(nRow).click(function () {
                if ($(this).hasClass('row_selected')) {
                    $(this).removeClass('row_selected');
                } else {
                    table.$('tr.row_selected').removeClass('row_selected');
                    $(this).addClass('row_selected');
                }
            });
        },
        "fnInitComplete": function (oSettings, json) {
        }
    });

});


//保存关联操作
function SaveRelationShip(obj){
    var AdminId = $(obj).attr("data-adminid");
    var JsonData = {AdminId : AdminId};
    var RoleIds = new Array();
    var dttable = $("#action_List").dataTable();
    var tableList = dttable.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
    $(tableList).each(function(k,v){
        var isSelected =  $(v).find("td").eq(0).find("i").hasClass("selected");
        if(isSelected == true){
            var RolleId =  $(v).find("td").eq(0).find("i").data("roleid");
            RoleIds.push(RolleId);

        }
    });
    JsonData.RoleIds = RoleIds;
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/admin-role-map-create',
        data: JsonData,
        success: function (ret) {
            if(ret.IsBizSuccess){
                layer.msg('保存操作成功！');
            }else{
                layer.msg('操作操作失败！');
            }
        }
    });
};



//选中
$(document).on('click','.themeicon-custom',function(){
    $(this).hasClass("selected") ? $(this).removeClass("selected") : $(this).addClass("selected");
});